/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 9/26/12
 * Time: 8:46 AM
 * To change this template use File | Settings | File Templates.
 */
package vplib.lab {
import mx.collections.ArrayCollection;

import vplib.component.AbstractPhysicsComponent;
import vplib.model.AbstractPhysicsModel;

public class Lab {
    private var labName:String;
    private var physicsComponents:ArrayCollection;

    public function Lab(name:String) {
        this.labName = name;
        this.physicsComponents = new ArrayCollection();
        trace("[Lab] \t- Create new instance with name '" + this.labName + "'.");
    }

    public function getPhysicsComponents():ArrayCollection {
        trace("[Lab] \t- Return objects from lab '" + this.labName + "'.");
        return this.physicsComponents;
    }

    public function addPhysicsComponent(apm:AbstractPhysicsComponent):void {
        this.physicsComponents.addItem(apm);
        this.prepareComponent(apm);
        //trace("[Lab] \t- Add object -> " + apm.toString());
    }

    public function editPhysicsComponent(apm:AbstractPhysicsComponent):void {
        var position:int = -1;
        for each (var item:AbstractPhysicsComponent in this.physicsComponents) {
            if (apm.uniqueId == item.uniqueId)
                position = this.physicsComponents.getItemIndex(item);
        }

        if (position != -1)
            this.physicsComponents.removeItemAt(position);

        this.physicsComponents.addItem(apm);

        //trace("[Lab] \t- Edit object -> " + apm.toString());
    }

    public function removePhysicsComponent(uniqueId:String):void {
        var position:int = -1;
        for each (var item:AbstractPhysicsComponent in this.physicsComponents) {
            if (uniqueId == item.uniqueId)
                position = this.physicsComponents.getItemIndex(item);
        }

        if (position != -1)
            this.physicsComponents.removeItemAt(position);

        trace("[Lab] \t- Remove object -> " + uniqueId);
    }

    public function prepareComponents():void {
        for each (var item:AbstractPhysicsComponent in this.physicsComponents) {
            item.applyModelToSurface();
        }
    }

    public function prepareComponent(item:AbstractPhysicsComponent):void {
        item.applyModelToSurface();
    }
}
}
